package com.lecture.java8.graph.demo01;

import lombok.Data;

import java.util.*;

@Data
public class GraphManager {
    private Map<Integer, Village> villages = new HashMap<>();
    private Map<Integer, List<Road>> adjacencyList = new HashMap<>();

    public void addVillage(Village village) {
        villages.put(village.id, village);
        adjacencyList.put(village.id, new ArrayList<>());
    }

    public void addRoad(Road road) {
        if (adjacencyList.containsKey(road.startId) && adjacencyList.containsKey(road.endId)) {
            adjacencyList.get(road.startId).add(road);
            adjacencyList.get(road.endId).add(new Road(road.endId, road.startId, road.length));
        }
    }

    public boolean isConnected() {
        Set<Integer> visited = new HashSet<>();
        dfs(visited, adjacencyList.keySet().iterator().next());
        return visited.size() == villages.size();
    }

    private void dfs(Set<Integer> visited, int vertex) {
        visited.add(vertex);
        for (Road road : adjacencyList.get(vertex)) {
            if (!visited.contains(road.endId)) {
                dfs(visited, road.endId);
            }
        }
    }

    public List<Integer> tsp(int startId) {
        // Implement TSP using brute force or heuristic algorithm
        return Collections.emptyList(); // Placeholder
    }

    public List<Integer> shortestPath(int startId) {
        // Implement Dijkstra's algorithm
        return Collections.emptyList(); // Placeholder
    }
}
